<template>
  <el-row>
    <el-col :span="20" :class="{ strong: isRoot }">{{ nodeData.name }}</el-col>
    <el-col :span="4">
      <el-row>
        <el-col :span="12">
          <span class="manager">{{ nodeData.manager }}</span>
        </el-col>
        <el-col :span="12">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="edit"
              >查看部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="del"
              >删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDept } from '@/api/department'
export default {
  name: 'TreeNode',
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleCommand(command) {
      switch (command) {
        case 'add':
          this.add()
          break
        case 'edit':
          this.edit()
          break
        case 'del':
          this.del()
          break
      }
    },
    add() {
      this.$emit('add', this.nodeData)
    },
    edit() {},
    async del() {
      try {
        await this.$confirm('是否删除此部门', '温馨提示', {
          type: 'warning'
        })
      } catch {
        return
      }
      await delDept(this.nodeData.id)
      this.$message.success('删除成功')
      this.$emit('getDept')
    }
  }
}
</script>

<style scoped lang="scss">
.el-col {
  height: 30px;
  line-height: 30px;
}
.el-row {
  width: 100%;
  font-size: 14px;
  color: #000;

  .strong {
    font-weight: 700;
  }
  .manager {
    text-align: right;
    display: block;
    padding-right: 20px;
  }
}
</style>
